package com.ives.launcher.view;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ives.launcher.R;
import com.ives.launcher.model.ItemInfo;
import com.ives.launcher.model.ItemInfo_App;
import com.ives.launcher.model.ItemInfo_Floder;

/**
 * 当点击文件夹时弹出对对话框
 * 
 * @author ives
 * 
 */
public class FloderDialog extends Dialog {
	private ItemInfo_Floder floder;
	private View_CustomDialog gridView;
	private EditText title;
	View_Cell cell;
	
	public FloderDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	public FloderDialog(Context context, int theme) {
		super(context, theme);
	}

	public FloderDialog(Context context) {
		super(context);
	}

	public FloderDialog(Context context, ItemInfo_Floder floder,View_Cell cell) {
		super(context);
		this.floder = floder;
		this.cell = cell;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom_dialog);
		gridView = (View_CustomDialog) findViewById(R.id.gridview);
		title = (EditText) findViewById(R.id.title);
		// 加载自定义的布局
		MyAdapter adapter = new MyAdapter(getContext());
		gridView.setAdapter(adapter);

		title.setText(floder.title);
		title.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				title.setFocusable(true);
				title.setFocusableInTouchMode(true);
			}
		});
		gridView.setTitle(title);
		gridView.setFloder(floder);
		gridView.setCell(cell);
	}


	public void addItem(ItemInfo info) {
		floder.items.add(info);
	}

	public void removeItem(ItemInfo info) {
		floder.items.remove(info);
	}
	
	// 自定义适配器
	class MyAdapter extends BaseAdapter {
		// 上下文对象
		Context context;

		// 构造方法
		MyAdapter(Context context) {
			this.context = context;
		}

		// 获得数量
		public int getCount() {
			return floder.items.size();
		}

		// 获得当前选项
		public Object getItem(int item) {
			return floder.items.get(item);
		}

		// 获得当前选项id
		public long getItemId(int id) {
			return id;
		}

		// 创建View方法
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageButton imageView = null;
//			if (convertView == null) {
				// 实例化ImageView对象
				ItemInfo info = floder.items.get(position);
				imageView = new ImageButton(context,info.icon,info.title.toString());
				imageView.setId(position);
//			} else {
//				imageView = (ImageButton) convertView;
//				System.out.println("---:test:"+imageView.title.getText());
//			}
			// 为ImageView设置图片资源
			imageView.setImage(floder.items.get(position).icon);
			final int index = position;
			imageView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (floder.items.get(index).itemType == ItemInfo.Type_App) {
						context.startActivity(new Intent(((ItemInfo_App) floder.items.get(index)).intent));
					}
				}
			});
			return imageView;
		}
	}

	public class ImageButton extends LinearLayout {
		public ImageView image;
		public TextView title;

		/**
		 * @param context
		 * @param attrs
		 */
		public ImageButton(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		/**
		 * @param context
		 */
		public ImageButton(Context context) {
			super(context);
		}

		public ImageButton(Context context, Bitmap bitmap, String str) {
			super(context);
			init(context, bitmap, str);
		}

		private void init(Context context, Bitmap bitmap, String str) {
			image = new ImageView(context);
			title = new TextView(context);
			title.setGravity(Gravity.CENTER_HORIZONTAL);

			image.setImageBitmap(bitmap);
			image.setPadding(0, 0, 0, 0);

			title.setText(str);
			title.setTextColor(Color.BLACK);
			title.setPadding(0, 0, 0, 0);

			// 设置本布局的属性
			setClickable(true); // 可点击
			setFocusable(true); // 可聚焦
			setBackgroundResource(android.R.drawable.btn_default); // 布局才用普通按钮的背景
			setOrientation(LinearLayout.VERTICAL); // 垂直布局

			// 首先添加Image，然后才添加Text
			// 添加顺序将会影响布局效果
			addView(image);
			addView(title);
			setBackgroundResource(R.drawable.floderborderstyle);
		}

		public void setImage(Bitmap bitmap) {
			image.setImageBitmap(bitmap);
		}

		public void setTitle(String str) {
			title.setText(str);
		}
	}

	/** getter and setter **/
	public ItemInfo_Floder getFloder() {
		return floder;
	}

	public void setFloder(ItemInfo_Floder floder) {
		this.floder = floder;
	}

}
